﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.ContentManagement.Handlers;
using RaisingStudio.Razor.Services;
using Orchard;
using Orchard.Environment.Features;
using System.Diagnostics;
using RaisingStudio.Trigger.Services;
using Orchard.ContentManagement;
using RaisingStudio.Trigger.Models;

namespace RaisingStudio.Trigger.Handlers
{
    public class TriggerUsingContentHandler : IContentHandler
    {
        private ITriggerService triggerService;
        private IOrchardServices orchardServices;

        public TriggerUsingContentHandler(IOrchardServices orchardServices, ITriggerService triggerService)
        {
            this.orchardServices = orchardServices;
            this.triggerService = triggerService;
        }

        private static TriggerModel ConvertModel(ContentItem contentItem, TriggerPart triggerPart)
        {
            TriggerModel triggerModel = new TriggerModel
            {
                Id = contentItem.Id,
                Title = triggerPart.Title,
                Event = triggerPart.Event,
                Condition = triggerPart.Condition,
                Action = triggerPart.Action
            };
            return triggerModel;
        }

        private bool IsTriggerLogEvent(ContentItem contentItem)
        {
            if ((contentItem != null) && (contentItem.ContentType == Migrations.TRIGGERLOGS_CONTENTTYPE))
            {
                return true;
            }
            return false;
        }

        public void Activating(ActivatingContentContext context)
        {
            this.triggerService.HandleEvent("Activating", this.orchardServices, null, context);
        }

        public void Activated(ActivatedContentContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("Activated", this.orchardServices, context.ContentItem, context);
        }

        public void Initializing(InitializingContentContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("Initializing", this.orchardServices, context.ContentItem, context);
        }

        public void Creating(CreateContentContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("Creating", this.orchardServices, context.ContentItem, context);
        }

        public void Created(CreateContentContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("Created", this.orchardServices, context.ContentItem, context);
        }

        public void Loading(LoadContentContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("Loading", this.orchardServices, context.ContentItem, context);
        }

        public void Loaded(LoadContentContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("Loaded", this.orchardServices, context.ContentItem, context);
        }

        public void Versioning(VersionContentContext context)
        {
            if (IsTriggerLogEvent(context.ExistingContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("Versioning", this.orchardServices, context.ExistingContentItem, context);
        }

        public void Versioned(VersionContentContext context)
        {
            if (IsTriggerLogEvent(context.ExistingContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("Versioned", this.orchardServices, context.ExistingContentItem, context);
        }

        public void Publishing(PublishContentContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("Publishing", this.orchardServices, context.ContentItem, context);
        }

        public void Published(PublishContentContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            if ((context.ContentItem != null) && (context.ContentItem.ContentType == Migrations.TRIGGERS_CONTENTTYPE))
            {
                ContentItem contentItem = context.ContentItem;
                TriggerPart triggerPart = contentItem.Parts.FirstOrDefault(p => p is TriggerPart) as TriggerPart;
                if (triggerPart != null)
                {
                    TriggerModel triggerModel = ConvertModel(contentItem, triggerPart);
                    this.triggerService.EnableTrigger(triggerModel, this.orchardServices);
                }
            }
            this.triggerService.HandleEvent("Published", this.orchardServices, context.ContentItem, context);
        }

        public void Unpublishing(PublishContentContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("Unpublishing", this.orchardServices, context.ContentItem, context);
        }

        public void Unpublished(PublishContentContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            if ((context.ContentItem != null) && (context.ContentItem.ContentType == Migrations.TRIGGERS_CONTENTTYPE))
            {
                ContentItem contentItem = context.ContentItem;
                TriggerPart triggerPart = contentItem.Parts.FirstOrDefault(p => p is TriggerPart) as TriggerPart;
                if (triggerPart != null)
                {
                    TriggerModel triggerModel = ConvertModel(contentItem, triggerPart);
                    this.triggerService.DisableTrigger(triggerModel, this.orchardServices);
                }
            }
            this.triggerService.HandleEvent("Unpublished", this.orchardServices, context.ContentItem, context);
        }

        public void Removing(RemoveContentContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("Removing", this.orchardServices, context.ContentItem, context);
        }

        public void Removed(RemoveContentContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            if ((context.ContentItem != null) && (context.ContentItem.ContentType == Migrations.TRIGGERS_CONTENTTYPE))
            {
                ContentItem contentItem = context.ContentItem;
                TriggerPart triggerPart = contentItem.Parts.FirstOrDefault(p => p is TriggerPart) as TriggerPart;
                if (triggerPart != null)
                {
                    TriggerModel triggerModel = ConvertModel(contentItem, triggerPart);
                    this.triggerService.RemoveTrigger(triggerModel, this.orchardServices);
                }
            }
            this.triggerService.HandleEvent("Removed", this.orchardServices, context.ContentItem, context);
        }

        public void Indexing(IndexContentContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("Indexing", this.orchardServices, context.ContentItem, context);
        }

        public void Indexed(IndexContentContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("Indexed", this.orchardServices, context.ContentItem, context);
        }

        public void Importing(ImportContentContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("Importing", this.orchardServices, context.ContentItem, context);
        }

        public void Imported(ImportContentContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("Imported", this.orchardServices, context.ContentItem, context);
        }

        public void Exporting(ExportContentContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("Exporting", this.orchardServices, context.ContentItem, context);
        }

        public void Exported(ExportContentContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("Exported", this.orchardServices, context.ContentItem, context);
        }


        public void Updating(UpdateContentContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("Updating", this.orchardServices, context.ContentItem, context);
        }

        public void Updated(UpdateContentContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("Updated", this.orchardServices, context.ContentItem, context);
        }


        public void GetContentItemMetadata(GetContentItemMetadataContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("GetContentItemMetadata", this.orchardServices, context.ContentItem, context);
        }

        public void BuildDisplay(BuildDisplayContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("BuildDisplay", this.orchardServices, context.ContentItem, context);
        }

        public void BuildEditor(BuildEditorContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            this.triggerService.HandleEvent("BuildEditor", this.orchardServices, context.ContentItem, context);
        }

        public void UpdateEditor(UpdateEditorContext context)
        {
            if (IsTriggerLogEvent(context.ContentItem))
            {
                return;
            }

            if ((context.ContentItem != null) && (context.ContentItem.ContentType == Migrations.TRIGGERS_CONTENTTYPE))
            {
                ContentItem contentItem = context.ContentItem;
                TriggerPart triggerPart = contentItem.Parts.FirstOrDefault(p => p is TriggerPart) as TriggerPart;
                if (triggerPart != null)
                {
                    TriggerModel triggerModel = ConvertModel(contentItem, triggerPart);
                    this.triggerService.RegisterTrigger(triggerModel, this.orchardServices);
                }
            }
            this.triggerService.HandleEvent("UpdateEditor", this.orchardServices, context.ContentItem, context);
        }

    }
}